package com.peycard.service;

import java.io.IOException;
import java.text.ParseException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.peycard.common.PortalUserDTO;
import com.peycard.util.Constantes;
import com.peycard.util.Utils;

@Service
public class LoginService extends BaseTransaction {

	public LoginService() throws IOException {
		super();
	}

	private static final Logger LOGGER = LoggerFactory
			.getLogger(LoginService.class);
	private String message;

	public PortalUserDTO loginRequest(String username) throws IOException,
			ParseException {
		PortalUserDTO currentUser = new PortalUserDTO();
		int requestId = 0;
		BaseTransaction baseLogin = initLoginValues();
		baseLogin.setField3_n6_idop(Constantes.ISO003_LOGIN);
		baseLogin.setField126_lllvar_metadata(Constantes.LOGIN_METADATA);
		baseLogin.setField127_lllvar_data(username);
		baseLogin.sendMessage();

		if (loginResponse(baseLogin)) {
			LOGGER.info(user(baseLogin, requestId).toString());
			currentUser = user(baseLogin, requestId);
		} else {
			message = Utils.getMessage(baseLogin);
		}
		return currentUser;
	}

	public String validateLogin(String username, String auditNumber)
			throws IOException, ParseException {
		String token = null;
		BaseTransaction base = initLoginValues();
		base.setField3_n6_idop(Constantes.ISO003_VALIDATE_LOGIN);
		base.setField11_n6_audit(auditNumber);
		base.setField126_lllvar_metadata(Constantes.LOGIN_METADATA);
		base.setField127_lllvar_data(username);
		base.sendMessage();

		if (loginResponse(base)) {
			LOGGER.info(base.getField123_resp_n1_first_access().toString());
			token = base.getField123_resp_n1_first_access();
		} else {
			message = Utils.getMessage(base);
		}
		return token;
	}

	public PortalUserDTO securityQuestionRequest(String email)
			throws IOException, ParseException {
		int requestId = 1;
		PortalUserDTO currentUser = new PortalUserDTO();
		BaseTransaction base = initLoginValues();
		base.setField3_n6_idop(Constantes.ISO003_SECURITY_QUESTION);
		base.setField126_lllvar_metadata(Constantes.RESTORE_METADATA);
		base.setField127_lllvar_data(email);
		base.sendMessage();

		if (loginResponse(base)) {
			LOGGER.info(user(base, requestId).toString());
			currentUser = user(base, requestId);
		} else {
			message = Utils.getMessage(base);
		}
		return currentUser;
	}

	public boolean validateRestorePassword(String idUser, String auditNumber)
			throws IOException, ParseException {
		boolean validation = true;
		BaseTransaction base = initLoginValues();
		base.setField3_n6_idop(Constantes.ISO003_VALIDATE_ANSWER);
		base.setField11_n6_audit(auditNumber);
		base.setField126_lllvar_metadata(Constantes.USER_ID_KEY);
		base.setField127_lllvar_data(idUser);
		base.sendMessage();

		if (!loginResponse(base)) {
			message = Utils.getMessage(base);
			validation = false;
		}
		return validation;
	}

	public boolean changePassword(String data) throws IOException,
			ParseException {
		boolean successful = true;
		BaseTransaction base = initLoginValues();
		base.setField3_n6_idop(Constantes.ISO003_CHANGE_PASSWORD);
		base.setField126_lllvar_metadata(Constantes.CHANGE_PWD_METADATA);
		base.setField127_lllvar_data(data);
		base.sendMessage();

		if (!loginResponse(base)) {
			message = Utils.getMessage(base);
			successful = false;
		}
		return successful;
	}

	private PortalUserDTO user(BaseTransaction base, int requestId) {
		Map<String, String> loginMap = Utils.baseMap(base);
		PortalUserDTO user = new PortalUserDTO();
		user.setAuditNumber(Integer.valueOf(base.getField11_resp_n6_audit()));
		switch (requestId) {
		case 0:
			int field123 = Integer.valueOf(base
					.getField123_resp_n1_first_access());
			user.setPassword(loginMap.get(Constantes.PASSWORD_KEY));
			user.setRol(loginMap.get(Constantes.ROL_KEY));
			user.setFirstAccess(field123);
			break;

		case 1:
			user.setIdPortalUser(Long.valueOf(loginMap
					.get(Constantes.USER_ID_KEY)));
			user.setAnswerQuestion(loginMap.get(Constantes.ANSWER_KEY));
			user.setSecurityQuestion(loginMap.get(Constantes.SECURITY_KEY));
		default:
			break;
		}
		return user;
	}

	private boolean loginResponse(BaseTransaction base) {
		String field39 = base.getField_39_resp_n2_cod_resp().toString();

		if (field39.equals(Constantes.ISO039_VALUE)) {
			String field41send = base.getField41_an16_idterminal();
			String field41resp = base.getField41_resp_an16_idterminal();
			if (field41send.equals(field41resp)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	private BaseTransaction initLoginValues() throws IOException,
			ParseException {
		BaseTransaction login = new LoginService();
		login.setField100_llvar_id_op_x(Constantes.ISO100_LOGIN);
		login.setField124_n1_token_present(Constantes.ISO0124_FIELD125_NO_PRESENT);
		return login;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
